package com.wbao.dianniu.manager;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.entity.StringEntity;

import android.content.Context;

import com.wbao.dianniu.data.MobileCheckData;
import com.wbao.dianniu.data.MobileData;
import com.wbao.dianniu.http.Batch;
import com.wbao.dianniu.http.Command;
import com.wbao.dianniu.http.HttpUtil;
import com.wbao.dianniu.http.HttpUtil.MyResponse;
import com.wbao.dianniu.listener.IMobileCheck;
import com.wbao.dianniu.listener.IMobileCheckListener;
import com.wbao.dianniu.logical.Action;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.utils.JsonUtil;

/**
 * 
 * @author yejianjun
 * 
 */
public class MobileCheckManager implements IMobileCheck {
	private IMobileCheckListener mListener;
	private Context mContext;

	public MobileCheckManager(Context context) {
		mContext = context;
	}

	@Override
	public void checkMobiles(int accountId, String mobiles) {
		// TODO Auto-generated method stub
		StringEntity stringEntity = null;
		try {
			Batch batch = new Batch();
			batch.setToken(GlobalContext.token);
			Command command = new Command();
			command.addAction(Action.ACTION_ACCOUNT_MOBILE_CHECK)
					.addArgs("accountId", accountId)
					.addArgs("mobiles", mobiles);
			batch.addCommand(command);
			stringEntity = new StringEntity(batch.toJson(), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		HttpUtil http = new HttpUtil();
//		http.req(mContext, stringEntity, new  myHandler(mContext));
		http.req(mContext, stringEntity, new MyResponse() {
			
			@Override
			public void responseSucess(Object obj) {
				// TODO Auto-generated method stub
				MobileCheckData mobileData = JsonUtil.toObject(obj, MobileCheckData.class);
				List<MobileData> list = new ArrayList<MobileData>();
				for (int i = 0; i < mobileData.joins.size(); i++) {
					MobileData data = new MobileData();
					data.setFlag(1);
					data.setMobile(mobileData.joins.get(i).getMobile());
					list.add(data);
				}
				for (int i = 0; i < mobileData.unjoins.size(); i++) {
					MobileData data = new MobileData();
					data.setFlag(0);
					data.setMobile(mobileData.unjoins.get(i));
					list.add(data);
				}
//				List<MobileData> list = JsonUtil.toObjectList(obj, MobileData.class);
				// 通知界面
				if (null != mListener)
					mListener.onMobileCheckSuccess(list);
			}
			
			@Override
			public void responseFailure(String msg) {
				// TODO Auto-generated method stub
				if (null != mListener)
					mListener.onMobileCheckFailure(0, msg);
			}
		});
	}

	@Override
	public boolean addMobileCheckListener(IMobileCheckListener listener) {
		mListener = listener;
		return mListener == null ? false : true;
	}

	@Override
	public boolean removeMobileCheckListener(IMobileCheckListener listener) {
		if (null == listener)
			return false;
		mListener = null;
		return true;
	};
	
}
